neutron.setTitle("Attribute")
neutron.setMinSize(240, 340)
neutron.setWinSize(240, 340)
var path = ""

permission_lst = {
    "owner_read": false,
    "owner_write": false,
    "owner_exec": false,
    "group_read": false,
    "group_write": false,
    "group_exec": false,
    "other_read": false,
    "other_write": false,
    "other_exec": false
}
function onscroll(event) { // 地址栏滚动事件
    var delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)));
    this.scrollLeft -= (delta * 30);
};
window.onload = function () {
    document.getElementById("filename").addEventListener("mousewheel", onscroll, false);

    var url = new URL(window.location.href); // 获取url参数
    path = url.searchParams.get("path"); // 获取文件路径
    document.getElementById("filename").innerText = path;
    setTimeout(() => {
        document.getElementById("filename").scrollLeft = document.getElementById("filename").scrollWidth // 滚动到最右边
    }, 0)
    var xhr = new XMLHttpRequest();
    xhr.open("GET", API_PATH + "/attribute?" + (new URLSearchParams({ "path": path }).toString()), true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var data = JSON.parse(xhr.responseText);
            if (data["code"] != 0) {
                alert(data["msg"])
            } else {
                // 基本信息
                var filesize = data["data"]["size"]["str"]
                var creationtime = data["data"]["time"]["creation"]
                var modificationtime = data["data"]["time"]["modification"]
                var owner = "[" + data["data"]["owner"]["uid"] + "]" + data["data"]["owner"]["name"]
                var group = "[" + data["data"]["group"]["gid"] + "]" + data["data"]["group"]["name"]
                var mode = data["data"]["mode"]
                document.getElementById("filesize").innerText = "文件大小: " + filesize
                document.getElementById("creationtime").innerText = "创建时间: " + creationtime
                document.getElementById("modificationtime").innerText = "修改时间: " + modificationtime
                document.getElementById("owner").innerText = "所有者: " + owner
                document.getElementById("group").innerText = "所属组: " + group
                oct_to_permission_lst(mode) // 计算模式并显示
                for (const [key, value] of Object.entries(permission_lst)) {
                    if (value) {
                        document.getElementById(key).classList.add("checked")
                    }
                }
                if (data["data"]["size"]["next"]) { // 需要异步计算
                    var ws = new WebSocket(API_PATH + "/attribute/size?" + (new URLSearchParams({ "path": path }).toString())); // ws请求
                    ws.onmessage = function (event) {
                        // 计算完成
                        var data = JSON.parse(event.data);
                        if (data["type"] != "result") {
                            return
                        }
                        document.getElementById("filesize").innerText = "文件夹大小: " + data["data"]["str"]
                    }
                }
            }
        }
    }
    xhr.send();
}


function get_oct_permission() { // 计算八进制权限
    var oct_permission = 0
    if (permission_lst["owner_read"]) {
        oct_permission += 400
    }
    if (permission_lst["owner_write"]) {
        oct_permission += 200
    }
    if (permission_lst["owner_exec"]) {
        oct_permission += 100
    }
    if (permission_lst["group_read"]) {
        oct_permission += 40
    }
    if (permission_lst["group_write"]) {
        oct_permission += 20
    }
    if (permission_lst["group_exec"]) {
        oct_permission += 10
    }
    if (permission_lst["other_read"]) {
        oct_permission += 4
    }
    if (permission_lst["other_write"]) {
        oct_permission += 2
    }
    if (permission_lst["other_exec"]) {
        oct_permission += 1
    }
    return oct_permission
}

function oct_to_permission_lst(oct) { // 八进制权限转权限列表
    const userPermissions = parseInt(oct[0], 8);
    const groupPermissions = parseInt(oct[1], 8);
    const othersPermissions = parseInt(oct[2], 8);
    if (userPermissions & 4) permission_lst["owner_read"] = true;
    if (userPermissions & 2) permission_lst["owner_write"] = true;
    if (userPermissions & 1) permission_lst["owner_exec"] = true;
    if (groupPermissions & 4) permission_lst["group_read"] = true;
    if (groupPermissions & 2) permission_lst["group_write"] = true;
    if (groupPermissions & 1) permission_lst["group_exec"] = true;
    if (othersPermissions & 4) permission_lst["other_read"] = true;
    if (othersPermissions & 2) permission_lst["other_write"] = true;
    if (othersPermissions & 1) permission_lst["other_exec"] = true;
}

function checkbox_event(event) { // 多选框事件
    // 选中状态
    if (document.getElementById(event.target.id).classList.contains("checked")) {
        document.getElementById(event.target.id).classList.remove("checked")
        permission_lst[event.target.id] = false
    } else {
        document.getElementById(event.target.id).classList.add("checked")
        permission_lst[event.target.id] = true
    }
    // 更新文件权限
    var permission_int = (get_oct_permission())
    var xhr = new XMLHttpRequest();
    xhr.open("POST", API_PATH + "/attribute/mode?" + (new URLSearchParams({ "path": path, "mode": permission_int }).toString()), true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var json = JSON.parse(xhr.responseText);
            if (json["code"] != 0) {
                alert(json["msg"])
            }
        }
    }
    xhr.send();
}

function change_owner() {
    var get_name = prompt("更改文件所有者")
    if (get_name == null || get_name == "") {
        return
    }
    // 获取用户uid
    var xhr = new XMLHttpRequest();
    xhr.open("GET", API_PATH + "/search_user_uid?" + (new URLSearchParams({ "username": get_name }).toString()), true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var json = JSON.parse(xhr.responseText);
            if (json["code"] != 0) { // 找不到用户
                alert(json["msg"])
                change_owner()
                return
            }
            if (json["data"] == -1) { // 无法获取uid
                return
            }
            var uid = json["data"]
            var xhr2 = new XMLHttpRequest();
            xhr2.open("POST", API_PATH + "/attribute/uid?" + (new URLSearchParams({ "path": path, "uid": json["data"] }).toString()), true); // 设置文件所有者
            xhr2.onreadystatechange = function () {
                if (xhr2.readyState == 4 && xhr2.status == 200) {
                    var json = JSON.parse(xhr2.responseText);
                    if (json["code"] != 0) {
                        alert(json["msg"])
                        return
                    }
                    document.getElementById("owner").innerText = "所有者: " + "[" + uid + "]" + get_name // 更新页面
                }
            }
            xhr2.send()
        }
    }
    xhr.send()
}

function change_group() {
    var get_name = prompt("更改文件组")
    if (get_name == null || get_name == "") {
        return
    }
    var xhr = new XMLHttpRequest();
    xhr.open("GET", API_PATH + "/search_group_gid?" + (new URLSearchParams({ "groupname": get_name }).toString()), true);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var json = JSON.parse(xhr.responseText);
            if (json["code"] != 0) {
                alert(json["msg"])
                change_group()
                return
            }
            if (json["data"] == -1) {
                return
            }
            var gid = json["data"]
            var xhr2 = new XMLHttpRequest();
            xhr2.open("POST", API_PATH + "/attribute/gid?" + (new URLSearchParams({ "path": path, "gid": json["data"] }).toString()), true);
            xhr2.onreadystatechange = function () {
                if (xhr2.readyState == 4 && xhr2.status == 200) {
                    var json = JSON.parse(xhr2.responseText);
                    if (json["code"] != 0) {
                        alert(json["msg"])
                        return
                    }
                    document.getElementById("group").innerText = "所属组: " + "[" + gid + "]" + get_name
                }
            }
            xhr2.send()
        }
    }
    xhr.send()
}

